package net.orange.console.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import javax.sql.rowset.CachedRowSet;

import net.orange.architecture.system.SystemParameters;
import net.orange.architecture.system.SystemSequence;
import net.orange.architecture.web.Web;
import net.orange.console.AnalyseTableWriter;
import net.orange.console.ConsoleExcelWriter;
import net.orange.console.ConsoleWriter;
import net.orange.console.HtmlAnalyseTableWriter;
import net.orange.console.JdbcConsole;
import net.orange.console.model.analyzer.AnalyseFilter;
import net.orange.console.model.analyzer.AnalyseTable;
import net.orange.console.model.analyzer.CalculationMethod;
import net.orange.console.model.dataset.ConsoleData;
import net.orange.console.model.dataset.ConsoleDataSet;
import net.orange.console.model.dataset.ConsoleMetaData;

/**
 * The Jdbc console managed bean using in JSF MVC modal. it is include the web
 * page content and control the action and page forward direction in the web
 * site. User can input the sql statement and retrieve the result through this
 * console.
 */
@ManagedBean(name="jdbc")
@SessionScoped
public class JdbcConsoleController {
	/**
	 * Default console datas page size.
	 */
	private static int MAX_DISPLAY_SIZE = 50;

	/**
	 * The jdbc driver class name.
	 */
	private String jdbcDriver;

	/**
	 * The database connectionURL;
	 */
	private String databaseURL;

	/**
	 * The database userName.
	 */
	private String userName;

	/**
	 * The database password.
	 */
	private String passWord;

	/**
	 * The program script
	 */
	private String sql;

	/**
	 * The result of program.
	 */
	private ConsoleDataSet datas;

	/**
	 * The export file url
	 */
	private String fileURL;

	/**
	 * The available AnalyseRow set.
	 */
	private Collection<SelectItem> availableAnalyseRowSet;

	/**
	 * The selected AnalyseRow set.
	 */
	private Collection<SelectItem> selectedAnalyseRowSet = new ArrayList<SelectItem>();

	/**
	 * The selected AvailableRow
	 */
	private String selecteAvailableRow;

	/**
	 * The selected AnalyzeRow
	 */
	private String selecteAnalyzeRow;

	/**
	 * The available AnalyseColumn set.
	 */
	private Collection<SelectItem> availableAnalyseColumnSet;

	/**
	 * The selected AnalyseColumn set.
	 */
	private Collection<SelectItem> selectedAnalyseColumnSet = new ArrayList<SelectItem>();

	/**
	 * The selected AvailableColumn
	 */
	private String selecteAvailableColumn;

	/**
	 * The selected AnalyzeColumn
	 */
	private String selecteAnalyzeColumn;

	/**
	 * The available CalculationField set.
	 */
	private Collection<SelectItem> availableCalculationFieldSet;

	/**
	 * The selected CalculationField.
	 */
	private String selectedCalculationField;

	/**
	 * The selected AnalyseMethod
	 */
	private String selectedAnalyseMethod;

	/**
	 * The available available AnalyseMethod set
	 */
	private Collection<SelectItem> availableAnalyseMethodSet = new ArrayList<SelectItem>();

	/**
	 * The available available AnalyseMethod set
	 */
	private Collection<SelectItem> availableAnalyseFilter;

	/**
	 * The selected the analyse filter.
	 */
	private String[] selectedAnalyseFilter;

	/**
	 * The console analyse table
	 */
	private AnalyseTable analyseTable;

	/**
	 * @return the sql
	 */
	public String getSql() {
		return sql;
	}

	/**
	 * @param sql
	 *            the sql to set
	 */
	public void setSql(String sql) {
		this.sql = sql;
	}

	/**
	 * @return the datas
	 */
	public ConsoleDataSet getDatas() {
		return datas;
	}

	/**
	 * @param datas
	 *            the datas to set
	 */
	public void setDatas(ConsoleDataSet datas) {
		this.datas = datas;
	}

	/**
	 * @return the jdbcDriver
	 */
	public String getJdbcDriver() {
		return (jdbcDriver == null || jdbcDriver.length() <= 0) ? "com.mysql.jdbc.Driver"
				: jdbcDriver;
	}

	/**
	 * @param jdbcDriver
	 *            the jdbcDriver to set
	 */
	public void setJdbcDriver(String jdbcDriver) {
		this.jdbcDriver = jdbcDriver;
	}

	/**
	 * @return the databaseURL
	 */
	public String getDatabaseURL() {
		return (databaseURL == null || databaseURL.length() <= 0) ? "jdbc:mysql://localhost:3306/test?zeroDateTimeBehavior=convertToNull"
				: databaseURL;
	}

	/**
	 * @param databaseURL
	 *            the databaseURL to set
	 */
	public void setDatabaseURL(String databaseURL) {
		this.databaseURL = databaseURL;
	}

	/**
	 * @return the userName
	 */
	public String getUserName() {
		return (userName == null || userName.length() <= 0) ? "root" : userName;
	}

	/**
	 * @param userName
	 *            the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 * @return the passWord
	 */
	public String getPassWord() {
		return (passWord == null || passWord.length() <= 0) ? "02280228"
				: passWord;
	}

	/**
	 * @param passWord
	 *            the passWord to set
	 */
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

	/**
	 * @return the fileURL
	 */
	public String getFileURL() {
		return fileURL;
	}

	/**
	 * @param fileURL
	 *            the fileURL to set
	 */
	public void setFileURL(String fileURL) {
		this.fileURL = fileURL;
	}

	/**
	 * @return the availableAnalyseRowSet
	 */
	public Collection<SelectItem> getAvailableAnalyseRowSet() {
		if (availableAnalyseRowSet == null) {
			if (datas != null) {
				availableAnalyseRowSet = new ArrayList<SelectItem>();

				for (Iterator iterator = datas.getMetaDataSetIterator(); iterator
						.hasNext();) {
					ConsoleMetaData metaData = (ConsoleMetaData) iterator
							.next();

					availableAnalyseRowSet.add(new SelectItem(metaData
							.getColumnName(), metaData.getColumnName()));
				}
			}
		}

		return availableAnalyseRowSet;
	}

	/**
	 * @param availableAnalyseRowSet
	 *            the availableAnalyseRowSet to set
	 */
	public void setAvailableAnalyseRowSet(
			Collection<SelectItem> availableAnalyseRowSet) {
		this.availableAnalyseRowSet = availableAnalyseRowSet;
	}

	/**
	 * @return the selectedAnalyseRowSet
	 */
	public Collection<SelectItem> getSelectedAnalyseRowSet() {
		return selectedAnalyseRowSet;
	}

	/**
	 * @param selectedAnalyseRowSet
	 *            the selectedAnalyseRowSet to set
	 */
	public void setSelectedAnalyseRowSet(
			Collection<SelectItem> selectedAnalyseRowSet) {
		this.selectedAnalyseRowSet = selectedAnalyseRowSet;
	}

	/**
	 * @return the selecteAvailableRow
	 */
	public String getSelecteAvailableRow() {
		return selecteAvailableRow;
	}

	/**
	 * @param selecteAvailableRow
	 *            the selecteAvailableRow to set
	 */
	public void setSelecteAvailableRow(String selecteAvailableRow) {
		this.selecteAvailableRow = selecteAvailableRow;
	}

	/**
	 * @return the selecteAnalyzeRow
	 */
	public String getSelecteAnalyzeRow() {
		return selecteAnalyzeRow;
	}

	/**
	 * @param selecteAnalyzeRow
	 *            the selecteAnalyzeRow to set
	 */
	public void setSelecteAnalyzeRow(String selecteAnalyzeRow) {
		this.selecteAnalyzeRow = selecteAnalyzeRow;
	}

	/**
	 * @return the availableAnalyseColumnSet
	 */
	public Collection<SelectItem> getAvailableAnalyseColumnSet() {
		if (availableAnalyseColumnSet == null) {
			if (datas != null) {
				availableAnalyseColumnSet = new ArrayList<SelectItem>();

				for (Iterator iterator = datas.getMetaDataSetIterator(); iterator
						.hasNext();) {
					ConsoleMetaData metaData = (ConsoleMetaData) iterator
							.next();
					if (!foundSelectItem(selectedAnalyseRowSet, metaData
							.getColumnName()))
						availableAnalyseColumnSet.add(new SelectItem(metaData
								.getColumnName(), metaData.getColumnName()));
				}
			}
		}
		return availableAnalyseColumnSet;
	}

	/**
	 * @param availableAnalyseColumnSet
	 *            the availableAnalyseColumnSet to set
	 */
	public void setAvailableAnalyseColumnSet(
			Collection<SelectItem> availableAnalyseColumnSet) {
		this.availableAnalyseColumnSet = availableAnalyseColumnSet;
	}

	/**
	 * @return the selectedAnalyseColumnSet
	 */
	public Collection<SelectItem> getSelectedAnalyseColumnSet() {
		return selectedAnalyseColumnSet;
	}

	/**
	 * @param selectedAnalyseColumnSet
	 *            the selectedAnalyseColumnSet to set
	 */
	public void setSelectedAnalyseColumnSet(
			Collection<SelectItem> selectedAnalyseColumnSet) {
		this.selectedAnalyseColumnSet = selectedAnalyseColumnSet;
	}

	/**
	 * @return the selecteAvailableColumn
	 */
	public String getSelecteAvailableColumn() {
		return selecteAvailableColumn;
	}

	/**
	 * @param selecteAvailableColumn
	 *            the selecteAvailableColumn to set
	 */
	public void setSelecteAvailableColumn(String selecteAvailableColumn) {
		this.selecteAvailableColumn = selecteAvailableColumn;
	}

	/**
	 * @return the selecteAnalyzeColumn
	 */
	public String getSelecteAnalyzeColumn() {
		return selecteAnalyzeColumn;
	}

	/**
	 * @param selecteAnalyzeColumn
	 *            the selecteAnalyzeColumn to set
	 */
	public void setSelecteAnalyzeColumn(String selecteAnalyzeColumn) {
		this.selecteAnalyzeColumn = selecteAnalyzeColumn;
	}

	/**
	 * @return the availableCalculationFieldSet
	 */
	public Collection<SelectItem> getAvailableCalculationFieldSet() {

		if (availableCalculationFieldSet == null) {
			if (datas != null) {
				availableCalculationFieldSet = new ArrayList<SelectItem>();

				for (Iterator iterator = datas.getMetaDataSetIterator(); iterator
						.hasNext();) {
					ConsoleMetaData metaData = (ConsoleMetaData) iterator
							.next();

					if (!foundSelectItem(selectedAnalyseRowSet, metaData
							.getColumnName())
							&& !foundSelectItem(selectedAnalyseColumnSet,
									metaData.getColumnName()))
						availableCalculationFieldSet.add(new SelectItem(
								metaData.getColumnName(), metaData
										.getColumnName()));

				}
			}
		}

		return availableCalculationFieldSet;
	}

	/**
	 * @param availableCalculationFieldSet
	 *            the availableCalculationFieldSet to set
	 */
	public void setAvailableCalculationFieldSet(
			Collection<SelectItem> availableCalculationFieldSet) {
		this.availableCalculationFieldSet = availableCalculationFieldSet;
	}

	/**
	 * @return the selectedCalculationField
	 */
	public String getSelectedCalculationField() {
		return selectedCalculationField;
	}

	/**
	 * @param selectedCalculationField
	 *            the selectedCalculationField to set
	 */
	public void setSelectedCalculationField(String selectedCalculationField) {
		this.selectedCalculationField = selectedCalculationField;
	}

	/**
	 * @return the availableAnalyseMethodSet
	 */
	public Collection<SelectItem> getAvailableAnalyseMethodSet() {
		if (availableAnalyseMethodSet == null
				|| availableAnalyseMethodSet.size() <= 0) {

			for (CalculationMethod method : CalculationMethod.values())
				availableAnalyseMethodSet.add(new SelectItem(method.name(),
						method.name()));
		}
		return availableAnalyseMethodSet;
	}

	/**
	 * @param availableAnalyseMethodSet
	 *            the availableAnalyseMethodSet to set
	 */
	public void setAvailableAnalyseMethodSet(
			Collection<SelectItem> availableAnalyseMethodSet) {
		this.availableAnalyseMethodSet = availableAnalyseMethodSet;
	}

	/**
	 * @return the selectedAnalyseMethod
	 */
	public String getSelectedAnalyseMethod() {
		return selectedAnalyseMethod;
	}

	/**
	 * @param selectedAnalyseMethod
	 *            the selectedAnalyseMethod to set
	 */
	public void setSelectedAnalyseMethod(String selectedAnalyseMethod) {
		this.selectedAnalyseMethod = selectedAnalyseMethod;
	}

	/**
	 * @return the analyseTable
	 */
	public AnalyseTable getAnalyseTable() {
		return analyseTable;
	}

	/**
	 * @param analyseTable
	 *            the analyseTable to set
	 */
	public void setAnalyseTable(AnalyseTable analyseTable) {
		this.analyseTable = analyseTable;
	}

	/**
	 * @return the availableAnalyseFilter
	 */
	public Collection<SelectItem> getAvailableAnalyseFilter() {
		if (availableAnalyseFilter == null) {
			if (datas != null) {
				availableAnalyseFilter = new ArrayList<SelectItem>();

				for (Iterator iterator = datas.getMetaDataSetIterator(); iterator
						.hasNext();) {
					ConsoleMetaData metaData = (ConsoleMetaData) iterator
							.next();
					availableAnalyseFilter.add(new SelectItem(metaData
							.getColumnName(), metaData.getColumnName()));
				}
			}
		}
		return availableAnalyseFilter;
	}

	/**
	 * @param availableAnalyseFilter
	 *            the availableAnalyseFilter to set
	 */
	public void setAvailableAnalyseFilter(
			Collection<SelectItem> availableAnalyseFilter) {
		this.availableAnalyseFilter = availableAnalyseFilter;
	}

	/**
	 * @return the selectedAnalyseFilter
	 */
	public String[] getSelectedAnalyseFilter() {
		return selectedAnalyseFilter;
	}

	/**
	 * @param selectedAnalyseFilter
	 *            the selectedAnalyseFilter to set
	 */
	public void setSelectedAnalyseFilter(String[] selectedAnalyseFilter) {
		this.selectedAnalyseFilter = selectedAnalyseFilter;
	}

	/**
	 * @return the analyseTableHtml
	 * @throws Expcetion
	 */
	public String getAnalyseTableHtml() throws Exception {

		analyseTable = new AnalyseTable(prepareAnalyseColumnSet(),
				prepareAnalyseRowSet(), prepareCalculationFieldSet(),
				prepareCalculationMethod(), prepareAnalyseFilters(),
				prepareFilterData(), datas);

		AnalyseTableWriter writer = new HtmlAnalyseTableWriter();

		return writer.getTableHtml(null, analyseTable);
	}

	/**
	 * @return the analyseFilterHtml
	 * @throws Expcetion
	 */
	public String getAnalyseFilterHtml() throws Exception {

		Collection<ConsoleMetaData> filters = prepareAnalyseFilters();
		Collection<ConsoleData> filtersData = prepareFilterData();
		Collection<AnalyseFilter> filterSet = new ArrayList<AnalyseFilter>();

		Object[] filterAry = filters.toArray();
		Object[] filterDataAry = filtersData.toArray();

		for (int i = 0; i < filterAry.length; ++i) {
			filterSet.add(new AnalyseFilter((ConsoleMetaData) filterAry[i],
					(ConsoleData) filterDataAry[i]));
		}
		AnalyseTableWriter writer = new HtmlAnalyseTableWriter();

		return writer.getFilterHtml(null, filterSet, filtersData, datas);
	}

	/**
	 * Execute the sql statement to retrieve the data.it limit at 150 rows data.
	 * If user need the whole data set, it can down load as excel.
	 * 
	 * @return The JSF form outcome. "execute" for ok,throw exception else.
	 * @throws Exception
	 */
	public String execute() throws Exception {
		cleanData();

		if (jdbcDriver == null || jdbcDriver.length() <= 0
				|| databaseURL == null || databaseURL.length() <= 0) {
			Web.setMessage(null, "Please select JDBC Drive / Database URL");
		}

		if (sql == null || sql.length() <= 0) {
			Web.setMessage(null, "No Sql statement found.");
		}

		try {
			JdbcConsole console = new JdbcConsole(jdbcDriver, databaseURL,
					userName, passWord);

			CachedRowSet rowSet = console.execute(sql, MAX_DISPLAY_SIZE);

			datas = new ConsoleDataSet(rowSet);

		} catch (Exception e) {
			Web.setMessage(null, "Error:" + e.getMessage());
			e.printStackTrace();
		}

		return Web.JSF_NAVIGATION_CONSOLE_EXECUTE;
	}

	/**
	 * Analyze the current console data set.
	 * 
	 * @return
	 * @throws Exception
	 */
	public String analyze() throws Exception {
		cleanData();

		try {
			JdbcConsole console = new JdbcConsole(jdbcDriver, databaseURL,
					userName, passWord);

			CachedRowSet rowSet = console.execute(sql);

			datas = new ConsoleDataSet(rowSet);

		} catch (Exception e) {
			Web.setMessage(null, "Error:" + e.getMessage());
			e.printStackTrace();
		}
		return "analyze";
	}

	/**
	 * Export the console data set to excel file, it will forward to the
	 * download page & let user down load the whole data set.
	 * 
	 * @return The JSF form outcome. "execute" for ok,throw exception else.
	 * @throws Exception
	 */
	public String export() throws Exception {
		try {
			JdbcConsole console = new JdbcConsole(jdbcDriver, databaseURL,
					userName, passWord);

			CachedRowSet rowSet = console.execute(sql);

			ConsoleDataSet dataSet = new ConsoleDataSet(rowSet);

			String filePath = SystemParameters.HTTP_RESOURCE_ROOT
					+ SystemParameters.RESOURCE_DATA_TMP;
			String fileName = Web.getCurrentUser().getLogin_no() + "_"
					+ String.valueOf(SystemSequence.getSequence()) + ".xls";

			fileURL = SystemParameters.HTTP_RESOURCE_URL
					+ SystemParameters.RESOURCE_DATA_TMP + fileName;

			ConsoleWriter writer = new ConsoleExcelWriter();
			writer.write(filePath + fileName, dataSet);

		} catch (Exception e) {
			Web.setMessage(null, "Error:" + e.getMessage());
			e.printStackTrace();

			return Web.JSF_NAVIGATION_CONSOLE_EXECUTE;
		}

		return Web.JSF_NAVIGATION_CONSOLE_EXPORT;
	}

	/**
	 * Select a field mete data from available AnalyseRow set to selected set.
	 * 
	 * @return The JSF form outcome. "addAnalyzeRow" for ok,throw exception
	 *         else.
	 * @throws Exception
	 */
	public String addAnalyzeRow() throws Exception {
		if (selecteAvailableRow != null && selecteAvailableRow.length() > 0) {

			selectedAnalyseRowSet.add(new SelectItem(selecteAvailableRow,
					selecteAvailableRow));

			for (SelectItem selectItem : availableAnalyseRowSet) {
				if (selectItem.getLabel().equals(selecteAvailableRow)) {
					availableAnalyseRowSet.remove(selectItem);
					break;
				}
			}
		}

		return "addAnalyzeRow";
	}

	/**
	 * Remove a field mete data from selected AnalyseRow set to available set.
	 * 
	 * @return The JSF form outcome. "addAnalyzeRow" for ok,throw exception
	 *         else.
	 * @throws Exception
	 */
	public String removeAnalyzeRow() throws Exception {
		if (selecteAnalyzeRow != null && selecteAnalyzeRow.length() > 0) {
			availableAnalyseRowSet.add(new SelectItem(selecteAnalyzeRow,
					selecteAnalyzeRow));

			for (SelectItem selectItem : selectedAnalyseRowSet) {
				if (selectItem.getLabel().equals(selecteAnalyzeRow)) {
					selectedAnalyseRowSet.remove(selectItem);
					break;
				}
			}
		}
		return "removeAnalyzeRow";
	}

	/**
	 * Select a field mete data from available AnalyseColumn set to selected
	 * set.
	 * 
	 * @return The JSF form outcome. "addAnalyzeColumn" for ok,throw exception
	 *         else.
	 * @throws Exception
	 */
	public String addAnalyzeColumn() throws Exception {
		if (selecteAvailableColumn != null
				&& selecteAvailableColumn.length() > 0) {

			selectedAnalyseColumnSet.add(new SelectItem(selecteAvailableColumn,
					selecteAvailableColumn));

			for (SelectItem selectItem : availableAnalyseColumnSet) {
				if (selectItem.getLabel().equals(selecteAvailableColumn)) {
					availableAnalyseColumnSet.remove(selectItem);
					break;
				}
			}
		}

		return "addAnalyzeColumn";
	}

	/**
	 * Remove a field mete data from selected AnalyseColumn set to available
	 * set.
	 * 
	 * @return The JSF form outcome. "addAnalyzeColumn" for ok,throw exception
	 *         else.
	 * @throws Exception
	 */
	public String removeAnalyzeColumn() throws Exception {
		if (selecteAnalyzeColumn != null && selecteAnalyzeColumn.length() > 0) {
			availableAnalyseColumnSet.add(new SelectItem(selecteAnalyzeColumn,
					selecteAnalyzeColumn));

			for (SelectItem selectItem : selectedAnalyseColumnSet) {
				if (selectItem.getLabel().equals(selecteAnalyzeColumn)) {
					selectedAnalyseColumnSet.remove(selectItem);
					break;
				}
			}
		}
		return "removeAnalyzeColumn";
	}

	/**
	 * Select the analyze row
	 * 
	 * @return The JSF form outcome. "selectAnalyzeRows_next" for
	 *         ok,"selectAnalyzeRows_error" else.
	 * @throws Exception
	 */
	public String selectAnalyzeRows() throws Exception {
		if (selectedAnalyseRowSet == null || selectedAnalyseRowSet.size() <= 0) {

			Web.setMessage(null, "Please select the items.");
			return "selectAnalyzeRows_error";
		}
		return "selectAnalyzeRows_next";
	}

	/**
	 * Select the analyze column.
	 * 
	 * @return The JSF form outcome. "selectAnalyzeColumns_next" for
	 *         ok,"selectAnalyzeColumns_error" else.
	 * @throws Exception
	 */
	public String selectAnalyzeColumns() throws Exception {
		if (selectedAnalyseColumnSet == null
				|| selectedAnalyseColumnSet.size() <= 0) {

			Web.setMessage(null, "Please select the items.");
			return "selectAnalyzeColumns_error";
		}
		return "selectAnalyzeColumns_next";
	}

	/**
	 * @return
	 * @throws Exception
	 */
	public String showAnalyzeTable() throws Exception {
		if (selectedCalculationField == null
				|| selectedCalculationField.length() <= 0
				|| selectedAnalyseMethod == null
				|| selectedAnalyseMethod.length() <= 0) {

			Web.setMessage(null,
					"Please select the calculation field & method.");
			return "selectedCalculationField_error";
		}

		return "showAnalyzeTable";
	}

	/**
	 * Check whether the select item collection contain the specify value.
	 * 
	 * @param selectItem
	 *            The original select item collection
	 * @param value
	 *            The specify value
	 * @return Whether the select item collection contain the specify value.
	 */
	private boolean foundSelectItem(Collection<SelectItem> selectItems,
			String value) {
		for (SelectItem selectItem : selectItems) {
			if (selectItem.getValue().equals(value))
				return true;
		}
		return false;
	}

	/**
	 * Prepare the analyse column set.
	 * 
	 * @return The current analyse column set.
	 */
	private Collection<ConsoleMetaData> prepareAnalyseColumnSet() {
		Map<String, ConsoleMetaData> metaDateSet = datas.getMetaDataSet();

		Collection<ConsoleMetaData> analyseColumnSet = new ArrayList<ConsoleMetaData>();

		for (SelectItem selectItem : selectedAnalyseColumnSet) {
			ConsoleMetaData metaData = metaDateSet.get(selectItem.getValue());
			analyseColumnSet.add(metaData);
		}

		return analyseColumnSet;
	}

	/**
	 * Prepare the analyse row set.
	 * 
	 * @return The current analyse row set.
	 */
	private Collection<ConsoleMetaData> prepareAnalyseRowSet() {
		Map<String, ConsoleMetaData> metaDateSet = datas.getMetaDataSet();

		Collection<ConsoleMetaData> analyseRowSet = new ArrayList<ConsoleMetaData>();

		for (SelectItem selectItem : selectedAnalyseRowSet) {
			ConsoleMetaData metaData = metaDateSet.get(selectItem.getValue());
			analyseRowSet.add(metaData);
		}

		return analyseRowSet;
	}

	/**
	 * Prepare the analyse row set.
	 * 
	 * @return The current analyse row set.
	 */
	private ConsoleMetaData prepareCalculationFieldSet() {
		Map<String, ConsoleMetaData> metaDateSet = datas.getMetaDataSet();
		return metaDateSet.get(selectedCalculationField);
	}

	/**
	 * Prepare the calculation method
	 * 
	 * @return The calculation method
	 */
	private CalculationMethod prepareCalculationMethod() {
		return CalculationMethod.valueOf(selectedAnalyseMethod);
	}

	/**
	 * Prepare the analyse filters.
	 * 
	 * @return The analyse filters.
	 */
	private Collection<ConsoleMetaData> prepareAnalyseFilters() {
		Map<String, ConsoleMetaData> metaDateSet = datas.getMetaDataSet();

		Collection<ConsoleMetaData> filterSet = new ArrayList<ConsoleMetaData>();

		for (String filter : selectedAnalyseFilter) {
			ConsoleMetaData metaData = metaDateSet.get(filter);
			filterSet.add(metaData);
		}

		return filterSet;
	}

	/**
	 * Prepare the analyse filters.
	 * 
	 * @return The analyse filters.
	 */
	private Collection<ConsoleData> prepareFilterData() throws Exception {
		Collection<ConsoleMetaData> filter = prepareAnalyseFilters();
		Collection<ConsoleData> filterData = new ArrayList<ConsoleData>();

		for (ConsoleMetaData metaData : filter) {
			String stringValue = (String) Web.getParameter("filter_"
					+ metaData.getColumnName());
			if (stringValue != null && stringValue.length() > 0)
				filterData.add(new ConsoleData(metaData, stringValue));
			else
				filterData.add(null);
		}

		return filterData;
	}

	/**
	 * Clean all the data.
	 */
	private void cleanData() {
		datas = null;

		availableAnalyseRowSet = null;
		selectedAnalyseRowSet.clear();
		selecteAvailableRow = "";
		selecteAnalyzeRow = " ";

		availableAnalyseColumnSet = null;
		selectedAnalyseColumnSet.clear();
		selecteAvailableColumn = "";
		selecteAnalyzeColumn = " ";

		availableCalculationFieldSet = null;
		selectedAnalyseMethod = "";
		selectedCalculationField = "";

		availableAnalyseFilter = null;
		selectedAnalyseFilter = null;
	}
}
